package cc.autoapi.pucong.auto.flow.provider.api.utils;

import cc.autoapi.pucong.auto.flow.provider.api.entity.vo.ApiParamsAddVo;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.selfenum.NodeTypeEnum;
import cc.autoapi.pucong.auto.flow.core.root.exception.AutoFlowException;
import cc.autoapi.pucong.auto.flow.core.root.selfenum.ApiTypeEnum;
import cc.autoapi.pucong.auto.flow.persistence.po.api.ApiInfoPo;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author pucong
 * @url https://gitee.com/pucong
 * @create 2023-05-31
 */
public class ApiDesignUtils {

    public static void main(String[] args) {
        testJson();
    }

    private static void testJson() {
        String json = "{\"cells\":[{\"shape\":\"edge\",\"attrs\":{\"line\":{\"stroke\":\"#A2B1C3\",\"targetMarker\":{\"name\":\"block\",\"width\":12,\"height\":8},\"strokeDasharray\":2,\"style\":{\"animation\":\"ant-line 30s infinite linear\"}}},\"id\":\"5a464677-b2e6-49eb-8601-64ab47b61ae9\",\"inherit\":\"edge\",\"zIndex\":0,\"source\":{\"cell\":\"1556ff72-fe5e-4e5d-9602-97d9961d0cbc\",\"port\":\"efdafda7-d08b-47ee-b17f-16d03fc7f4ee\"},\"target\":{\"cell\":\"e230ae6b-922b-4811-9c19-0c9a4826d3e3\",\"port\":\"in-port\"}},{\"shape\":\"edge\",\"attrs\":{\"line\":{\"stroke\":\"#A2B1C3\",\"targetMarker\":{\"name\":\"block\",\"width\":12,\"height\":8},\"strokeDasharray\":2,\"style\":{\"animation\":\"ant-line 30s infinite linear\"}}},\"id\":\"903dfc7f-e6cd-4905-8b07-c8b8c7043e2f\",\"inherit\":\"edge\",\"zIndex\":0,\"source\":{\"cell\":\"e230ae6b-922b-4811-9c19-0c9a4826d3e3\",\"port\":\"right-port-true\"},\"target\":{\"cell\":\"4c669207-52aa-4642-8fc8-7097bd60482a\",\"port\":\"e590221b-ddb3-41ec-84ff-7b88810bd086\"}},{\"shape\":\"edge\",\"attrs\":{\"line\":{\"stroke\":\"#A2B1C3\",\"targetMarker\":{\"name\":\"block\",\"width\":12,\"height\":8},\"strokeDasharray\":2,\"style\":{\"animation\":\"ant-line 30s infinite linear\"}}},\"id\":\"6e8c3207-c39a-4d71-b42c-afcba12bbd2c\",\"inherit\":\"edge\",\"zIndex\":0,\"source\":{\"cell\":\"e230ae6b-922b-4811-9c19-0c9a4826d3e3\",\"port\":\"right-port-false\"},\"target\":{\"cell\":\"04fa547b-58c5-4f26-a991-0f500295a50f\",\"port\":\"8891deaf-1b42-4dbb-8810-db57a1c5f3bb\"}},{\"position\":{\"x\":290,\"y\":90},\"size\":{\"width\":430,\"height\":470},\"view\":\"vue-shape-view\",\"shape\":\"custom-vue-node\",\"ports\":{\"groups\":{\"top\":{\"position\":\"top\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"right\":{\"position\":\"right\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"bottom\":{\"position\":\"bottom\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"left\":{\"position\":\"left\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}}},\"items\":[{\"group\":\"top\",\"id\":\"5852f305-7ab0-4a67-8c48-4d747b11cc59\"},{\"group\":\"right\",\"id\":\"efdafda7-d08b-47ee-b17f-16d03fc7f4ee\"},{\"group\":\"bottom\",\"id\":\"b52e5b2d-9c28-4036-ba63-e36121bf43a8\"},{\"group\":\"left\",\"id\":\"b28908be-1077-4ba7-971f-0a8dec1e0552\"}]},\"id\":\"1556ff72-fe5e-4e5d-9602-97d9961d0cbc\",\"data\":{\"nodeCode\":\"apiConfig\",\"nodeName\":\"api配置节点\",\"paramInAuto\":true,\"paramsInList\":[],\"paramsOutList\":[],\"label\":\"api配置节点aa\",\"desc\":\"api配置节点\"},\"zIndex\":1},{\"position\":{\"x\":920,\"y\":160},\"size\":{\"width\":430,\"height\":440},\"view\":\"vue-shape-view\",\"shape\":\"custom-check-node\",\"ports\":{\"groups\":{\"left\":{\"position\":\"left\",\"label\":{\"position\":{\"name\":\"outside\"}},\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"right\":{\"position\":\"right\",\"label\":{\"position\":{\"name\":\"outside\"}},\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}}},\"items\":[{\"id\":\"in-port\",\"group\":\"left\"},{\"id\":\"right-port-true\",\"group\":\"right\",\"attrs\":{\"text\":{\"text\":\"真\"}}},{\"id\":\"right-port-false\",\"group\":\"right\",\"attrs\":{\"text\":{\"text\":\"假\"}}}]},\"id\":\"e230ae6b-922b-4811-9c19-0c9a4826d3e3\",\"data\":{\"nodeCode\":\"if\",\"nodeName\":\"条件判断节点\",\"paramInAuto\":false,\"paramsInList\":[{\"paramCode\":\"booleanCheck\",\"dateType\":7,\"defaultValue\":\"true\",\"needValue\":true,\"desc\":\"布尔判断，可以是对象、布尔值、字符串\"}],\"paramsOutList\":[],\"label\":\"条件判断节点\",\"desc\":\"条件判断节点\"},\"zIndex\":2},{\"position\":{\"x\":1470,\"y\":-40},\"size\":{\"width\":430,\"height\":500},\"view\":\"vue-shape-view\",\"shape\":\"custom-vue-node\",\"ports\":{\"groups\":{\"top\":{\"position\":\"top\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"right\":{\"position\":\"right\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"bottom\":{\"position\":\"bottom\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"left\":{\"position\":\"left\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}}},\"items\":[{\"group\":\"top\",\"id\":\"e8000ad6-9a30-4b69-9528-e9cd2b99419d\"},{\"group\":\"right\",\"id\":\"64a0b453-a39f-41e4-95fc-a02cd99d467b\"},{\"group\":\"bottom\",\"id\":\"1b8c2663-77aa-45b5-80ce-0c03631e58e1\"},{\"group\":\"left\",\"id\":\"e590221b-ddb3-41ec-84ff-7b88810bd086\"}]},\"id\":\"4c669207-52aa-4642-8fc8-7097bd60482a\",\"data\":{\"nodeCode\":\"common\",\"nodeName\":\"通用任务\",\"paramInAuto\":false,\"paramsInList\":[{\"paramCode\":\"paramCode\",\"dateType\":1,\"defaultValue\":1,\"needValue\":true,\"desc\":\"\"}],\"paramsOutList\":[{\"paramCode\":\"paramCode\",\"dateType\":1,\"desc\":\"\"}],\"label\":\"通用任务\",\"desc\":\"通用任务\"},\"zIndex\":3},{\"position\":{\"x\":1480,\"y\":590},\"size\":{\"width\":430,\"height\":500},\"view\":\"vue-shape-view\",\"shape\":\"custom-vue-node\",\"ports\":{\"groups\":{\"top\":{\"position\":\"top\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"right\":{\"position\":\"right\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"bottom\":{\"position\":\"bottom\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}},\"left\":{\"position\":\"left\",\"attrs\":{\"circle\":{\"r\":4,\"magnet\":true,\"stroke\":\"#5F95FF\",\"strokeWidth\":1,\"fill\":\"#fff\",\"style\":{\"visibility\":\"hidden\"}}}}},\"items\":[{\"group\":\"top\",\"id\":\"81628ccb-c2ad-42f8-bbf6-4082fbba7bd5\"},{\"group\":\"right\",\"id\":\"99e196b4-e136-4e82-99d5-11ef2dc2195f\"},{\"group\":\"bottom\",\"id\":\"c108b8fa-67d7-4181-baf5-14272493398a\"},{\"group\":\"left\",\"id\":\"8891deaf-1b42-4dbb-8810-db57a1c5f3bb\"}]},\"id\":\"04fa547b-58c5-4f26-a991-0f500295a50f\",\"data\":{\"nodeCode\":\"common\",\"nodeName\":\"通用任务\",\"paramInAuto\":false,\"paramsInList\":[{\"paramCode\":\"paramCode\",\"dateType\":1,\"defaultValue\":1,\"needValue\":true,\"desc\":\"\"}],\"paramsOutList\":[{\"paramCode\":\"paramCode\",\"dateType\":1,\"desc\":\"\"}],\"label\":\"通用任务\",\"desc\":\"通用任务\"},\"zIndex\":4}]}";
        ApiDesignUtils.dealJson2List(json,
                new ApiInfoPo(), new ArrayList<>());
    }

    public static void dealJson2List(String jsonStr,
                                     ApiInfoPo apiInfo,
                                     List<ApiParamsAddVo> apiParamsAdds) {
        if (StringUtils.isBlank(jsonStr)) {
            return;
        }
        JSONObject json = JSONObject.parseObject(jsonStr);
        JSONArray cells = json.getJSONArray("cells");
        if (cells == null) {
            return;
        }
        for (Object cell : cells) {
            JSONObject cellJson = (JSONObject) cell;
            String shape = cellJson.getString("shape");
            String id = cellJson.getString("id");
            if (shape.equals("custom-vue-node") || shape.equals("custom-check-node")|| shape.equals("custom-tools-node")) {
                JSONObject data = cellJson.getJSONObject("data");
                String parentId = cellJson.getString("parent");
                String processId = data.getString("processId");
                Integer forDataType = data.getInteger("forDataType");
                String forValue = data.getString("forValue");
                Boolean forAsync = data.getBoolean("forAsync");
                String nodeCode = data.getString("nodeCode");
                String script = data.getString("script");
                String tag = data.getString("tag");
                String label = data.getString("label");
                String desc = data.getString("desc");
                JSONArray paramsInList = data.getJSONArray("paramsInList");
                JSONArray paramsOutList = data.getJSONArray("paramsOutList");

                if (nodeCode.equals(NodeTypeEnum.API_CONFIG.getKey())) {
                    String apiCode = data.getString("apiCode");
                    Integer apiType = data.getInteger("apiType");
                    if (StringUtils.isBlank(apiCode) || apiType == null) {
                        throw new AutoFlowException("请配置API信息，api组件的编码和apiType不能为空");
                    }
                    apiInfo.setApiCode(apiCode);
                    apiInfo.setApiType(ApiTypeEnum.getItemByValue(apiType));
                    apiInfo.setServiceType(0);
                    apiInfo.setApiDesc(desc);
                }
                if (nodeCode.equals(NodeTypeEnum.CRON_CONFIG.getKey())) {
                    String apiCorn = data.getString("apiCorn");
                    apiInfo.setApiCorn(apiCorn);
                    apiInfo.setServiceType(1);
                }
                if (paramsInList != null) {
                    for (Object o : paramsInList) {
                        JSONObject jsonObject = (JSONObject) o;
                        String paramCode = jsonObject.getString("paramCode");
                        String paramValue = jsonObject.getString("paramValue");
                        Integer paramDataType = jsonObject.getInteger("paramDataType");
                        Integer dateType = jsonObject.getInteger("dateType");
                        Integer requestPosition = jsonObject.getInteger("paramPosition");
                        Boolean needValue = jsonObject.getBooleanValue("needValue");
                        String desc2 = jsonObject.getString("desc");

                        if (nodeCode.equals(NodeTypeEnum.API_CONFIG.getKey())) {
                            ApiParamsAddVo apiParamsAddDto = new ApiParamsAddVo();
                            apiParamsAddDto.setParamCode(paramCode);
                            apiParamsAddDto.setRequestPosition(requestPosition);
                            apiParamsAddDto.setDateType(dateType);
                            apiParamsAddDto.setParamType(0);
                            apiParamsAddDto.setNeedValue(needValue ? 1 : 0);
                            apiParamsAddDto.setParamDesc(desc2);
                            apiParamsAdds.add(apiParamsAddDto);
                        }
                    }
                }
                if (paramsOutList != null) {
                    for (Object o : paramsOutList) {
                        JSONObject jsonObject = (JSONObject) o;
                        String paramCode = jsonObject.getString("paramCode");
                        Integer dateType = jsonObject.getInteger("dateType");
                        Boolean needValue = jsonObject.getBooleanValue("needValue");
                        String desc2 = jsonObject.getString("desc");
                        Boolean returnValue = jsonObject.getBooleanValue("returnValue");

                        if (nodeCode.equals(NodeTypeEnum.API_CONFIG.getKey())) {
                            ApiParamsAddVo apiParamsAddDto = new ApiParamsAddVo();
                            apiParamsAddDto.setParamCode(paramCode);
                            apiParamsAddDto.setDateType(dateType);
                            apiParamsAddDto.setParamType(1);
                            apiParamsAddDto.setNeedValue(needValue ? 1 : 0);
                            apiParamsAddDto.setParamDesc(desc2);
                            apiParamsAdds.add(apiParamsAddDto);
                        }
                    }
                }
            }
        }
    }


    public static List<String> getJsonIdList(String serviceContent) {
        List<String> list = new ArrayList<>();
        if (StringUtils.isBlank(serviceContent)) {
            return list;
        }
        JSONObject json = JSONObject.parseObject(serviceContent);
        JSONArray cells = json.getJSONArray("cells");
        if (cells == null) {
            return list;
        }
        for (Object cell : cells) {
            JSONObject cellJson = (JSONObject) cell;
            String shape = cellJson.getString("shape");
            String id = cellJson.getString("id");
            list.add(id);
            if (shape.equals("edge")) {
                JSONObject source = cellJson.getJSONObject("source");
                String sourceId = source.getString("cell");

                JSONObject target = cellJson.getJSONObject("target");
                String targetId = target.getString("cell");
                if (StringUtils.isBlank(targetId)) {
                    continue;
                }
                if (StringUtils.isBlank(sourceId)) {
                    continue;
                }
                list.add(sourceId);
                list.add(targetId);
            } else if (shape.equals("custom-vue-node") || shape.equals("custom-tools-node") || shape.equals("custom-check-node")) {
                JSONObject data = cellJson.getJSONObject("data");
                JSONArray paramsIns = data.getJSONArray("paramsIns");

                if (paramsIns != null) {
                    for (int i = 0; i < paramsIns.size(); i++) {
                        Object cell2 = paramsIns.get(i);
                        JSONObject paramsIn = (JSONObject) cell2;
                        String paramValue = paramsIn.getString("paramValue");
                        // 参数类型 0静态值 1前置接口返回值 2全局参数
                        Integer paramDataType = paramsIn.getInteger("paramDataType");

                        if (paramDataType == 1) {
                            list.add(paramValue);
                        }
                    }
                }
            }
        }
        return list;
    }
}
